﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL.Admin;
using System.Data.SqlClient;
using DBUtility;

public partial class Admin_addFunction : System.Web.UI.Page
{
    /// <summary>
    /// 功列表
    /// </summary>
    private void bindGrid()
    {
        SqlParameter[] parms = new SqlParameter[]
          {
             new SqlParameter ("@ALID",SqlDbType .Int)
          };
        parms[0].Value = Convert.ToInt32(Request.QueryString["ALID"]);
        DataTable dt_function = SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocal, CommandType.StoredProcedure, "S_selectAllFunctionAFIDByALID", parms).Tables[0];
        //上面是查询出来所有在这个权限里面的功能
        //给存储过程传参数
  

        GVAdminFumction.DataSource = AdminServices.SP_select("AdminFunction", "*");
        GVAdminFumction.DataBind();
        for (int i = 0; i < GVAdminFumction.Rows.Count; i++)
        {
            for (int j = 0; j < dt_function.Rows.Count; j++)
            {
                if (GVAdminFumction.Rows[i].Cells[0].Text == dt_function.Rows[j][0].ToString().Trim())
                {
                    LinkButton1.Enabled = true;
                    ((CheckBox)GVAdminFumction.Rows[i].Cells[4].FindControl("CBISAdd")).Checked = true;
                }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["admin"].ToString().Trim() != "1")
            {
                Response.Redirect("Login.aspx");
            }
            //没有什么实际意义主要是测试用
            if (!IsPostBack)
            {
                bindGrid();
            }
        }
        catch (Exception)
        {

        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        try
        {
            //先全部删除现在的权限
            if (AdminServices.SP_delete("[Function]", "ALID=" + Convert.ToInt32(Request.QueryString["ALID"])))
            {
                //再添加新的功能
                for (int i = 0; i < GVAdminFumction.Rows.Count; i++)
                {
                    if (((CheckBox)GVAdminFumction.Rows[i].Cells[4].FindControl("CBISAdd")).Checked == true)
                    {
                        SqlParameter[] parms = new SqlParameter[]
                     {
                         new SqlParameter ("@ALID",SqlDbType .Int),
                         new SqlParameter ("@AFID",SqlDbType .Int)
                     };
                        parms[0].Value = Convert.ToInt32(Request.QueryString["ALID"]);
                        parms[1].Value = Convert.ToInt32(GVAdminFumction.Rows[i].Cells[0].Text.ToString().Trim());
                        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocal, CommandType.StoredProcedure, "S_insertOneFunction", parms);
                    }
                }
                Response.Redirect("FunctionManageMent.aspx");
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = "发生异常：" + ex.Message.ToString().Trim();
        }
    }
}
